﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Components.Authorization;
using System.Runtime.CompilerServices;
using System.Security.Claims;

namespace BootstrapBlazor.Test.Data
{
    public class MyAuthenticationServiceProvider : AuthenticationStateProvider
    {
        private bool IsLogin = false;
        private IConfiguration configuration;

        public MyAuthenticationServiceProvider(IConfiguration _configuration)
        {
            configuration = _configuration;
        }

        public override Task<AuthenticationState> GetAuthenticationStateAsync()
        {
            if (IsLogin)
            {
                var claims = new List<Claim> { new Claim(ClaimTypes.Name, "Admin"), new Claim(ClaimTypes.NameIdentifier, "Admin") };

                //var cp = new ClaimsIdentity(claims, logininfo.Domain);
                var props = new AuthenticationProperties
                {
                    IsPersistent = true,
                    ExpiresUtc = DateTimeOffset.UtcNow.Add(TimeSpan.FromDays(1))
                };
                var cp = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                var state = new AuthenticationState(new ClaimsPrincipal(cp));
                return Task.FromResult(state);
            }
            else
            {
                var anonymous = new ClaimsIdentity();
                return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(anonymous)));
            }
        }

        public bool SignIn(string username, string password, string domain)
        {
            var res = false;
            res = username.Length > 0 && password.Length > 0;
            IsLogin = res;
            NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
            return res;
        }



        public void SignOut()
        {
            IsLogin = false;
            NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
        }

    }
}
